﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AddSteps.cs" company="">
//   
// </copyright>
// <summary>
//   The add steps.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace UITest.SpecFlowWithCUIT
{
    using System;
    using System.Globalization;
    using System.Text;

    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using TechTalk.SpecFlow;

    using UITest.Report;

    /// <summary>
    ///     The add steps.
    /// </summary>
    [Binding]
    public class AddSteps
    {
        #region Fields

        /// <summary>
        /// The _aut.
        /// </summary>
        private readonly ApplicationUnderTest _aut;

        /// <summary>
        /// The _random.
        /// </summary>
        private readonly Random _random = new Random();

        /// <summary>
        /// The test context.
        /// </summary>
        private TestContext TestContext;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="AddSteps" /> class.
        /// </summary>
        public AddSteps()
        {
            this._aut = ApplicationUnderTest.Launch(@"C:\Windows\System32\calc.exe");
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets the random token.
        /// </summary>
        public string RandomToken
        {
            get
            {
                return this.GetRandomToken();
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     The close test case.
        /// </summary>
        public void CloseTestCase()
        {
        }

        /// <summary>
        /// The given i have entered into the calculator.
        /// </summary>
        /// <param name="p0">
        /// The p 0.
        /// </param>
        [Given(@"I have entered (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(int p0)
        {
            Keyboard.SendKeys(this._aut, this.NumbersToSendKeysString(p0));
        }

        /// <summary>
        ///     The given i press add.
        /// </summary>
        [Given(@"I press add")]
        public void GivenIPressAdd()
        {
            Keyboard.SendKeys(this._aut, "{Add}");
        }

        /// <summary>
        ///     The test cleanup.
        /// </summary>
        [AfterScenario]
        public void TestCleanup()
        {
            this.TestContext = (TestContext)FeatureContext.Current["TestContext"];
            this.CloseTestCase();
            this.TestContext = null;
            ReportFactory.Dispose();
        }

        /// <summary>
        ///     The test init.
        /// </summary>
        [BeforeScenario]
        public void TestInit()
        {
            ReportFactory.New();
        }

        /// <summary>
        /// The then the result should be on the screen.
        /// </summary>
        /// <param name="result">
        /// The result.
        /// </param>
        [Then(@"the result should be (.*) on the screen")]
        public void ThenTheResultShouldBeOnTheScreen(int result)
        {
            var resultTextBox = new WinText(this._aut);
            resultTextBox.SearchProperties[UITestControl.PropertyNames.Name] = "Result";

            Assert.AreEqual(result.ToString(CultureInfo.InvariantCulture), resultTextBox.DisplayText);
        }

        /// <summary>
        ///     The when i press enter.
        /// </summary>
        [When(@"I press enter")]
        public void WhenIPressEnter()
        {
            Keyboard.SendKeys(this._aut, "{Enter}");
        }

        #endregion

        #region Methods

        /// <summary>
        /// The numbers to send keys string.
        /// </summary>
        /// <param name="number">
        /// The number.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        protected string NumbersToSendKeysString(int number)
        {
            var result = new StringBuilder();
            char[] numbers = number.ToString(CultureInfo.InvariantCulture).ToCharArray();

            foreach (char c in numbers)
            {
                result.AppendFormat("{{NumPad{0}}}", c);
            }

            return result.ToString();
        }

        /// <summary>
        /// The get random token.
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string GetRandomToken()
        {
            char[] baseChars =
                {
                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 
                    'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
                    'Y', 'Z'
                };
            string result = string.Empty;
            int targetBase = baseChars.Length;
            var epoch = new DateTime(2015 - this._random.Next(0, 100), 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var value = (long)(DateTime.UtcNow - epoch).TotalMilliseconds;

            // long value = (long)36 * 36 * 36 * 36 * 36 * 36 * 36 * 36 * 36 - 1;
            do
            {
                result = baseChars[value % targetBase] + result;
                value = value / targetBase;
            }
            while (value > 0);

            return result;
        }

        #endregion
    }
}